//
//  AFNetWorking.m
//  testpbl
//
//  Created by 黄权浩 on 15/8/10.
//  Copyright (c) 2015年 黄权浩. All rights reserved.
//

#import "AFNetWorking.h"

@implementation AFNetWorking

/**************************************************************
     ***************使用方法***************
 
 //    [AFNetWorking afnetworkingPost:nil posturl:URL changeDic:^(NSDictionary *dic) {
 //        NSLog(@"%@", dic);   ->block回调//做数据处理
 //    }];
     
     ***************POST请求***********************
 ************************************************************/



//get请求
+ (void)afnetworkingGet:(NSDictionary *)data geturl:(NSString *)url changeDic:(ChangeNSDictionary)changeDic
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        changeDic(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        //这个后续根据服务器设置而设置
    }];
}

//post请求
+ (void)afnetworkingPost:(NSDictionary *)data posturl:(NSString *)url changeDic:(ChangeNSDictionary)changeDic
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        changeDic(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        //这个后续根据服务器设置而设置
    }];
}

//网络监测
+ (void)reach:(ChangeState)changestate
{
    /**
     AFNetworkReachabilityStatusUnknown          = -1,  // 未知<未知网络属性>
     AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接<未连接到网络>
     AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 4G
     AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络<wift>
     **/
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld", (long)status);
        changestate(status);
    }];
}

//创建下载任务
+ (void)sessionDownload:(NSString *)downloadUrl backPath:(DownloadPath)pathurl
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:downloadUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        pathurl([documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]);
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"File downloaded to: %@", filePath);
    }];
    [downloadTask resume];
}

//创建上传任务
+ (void)sessionUpload:(NSString *)unloadUrl serverPath:(NSString *)serverUrl backstate:(UnloadState)unloadstate
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:serverUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURL *filePath = [NSURL fileURLWithPath:unloadUrl];
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
            unloadstate(NO);
        } else {
            NSLog(@"Success: %@ %@", response, responseObject);
            unloadstate(YES);
        }
    }];
    [uploadTask resume];
}


@end
